﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using FinalSilverlightProject.Web.Models;
using FinalSilverlightProject.Web.Services;

namespace FinalSilverlightProject.Views.Administrator
{

    public partial class Accounts : Page
    {
        private readonly AdministratorContext administratorContext = new AdministratorContext();
        private InvokeOperation<ObservableCollection<AdministratorEditRegistrationData>> allUsers;
        public Accounts()
        {
            InitializeComponent();

        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //if (!WebContext.Current.User.IsAuthenticated || !WebContext.Current.User.IsInRole("Administrators"))
            //{
            //    (this.Parent as Frame).Navigate(new Uri("/Home", UriKind.Relative));
            //}
            //else (this.Content as UIElement).Visibility = System.Windows.Visibility.Visible;
            accountAnimation.Begin();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                this.busyIndicator.IsBusy = true;
                allUsers = administratorContext.GetAllUsers();
                allUsers.Completed += (s, ex) =>
                 {
                     this.busyIndicator.IsBusy = false;
                     if (!allUsers.HasError)
                     {
                        dataPager1.Source = allUsers.Value;
                        dataGridUsers.ItemsSource = allUsers.Value;
                     }
                 };
            }
        }

        private void dataGridUsers_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.PropertyName == "City" || e.PropertyName == "Country" || e.PropertyName == "PostCode"
                 || e.PropertyName == "StreetAddress" || e.PropertyName == "TelephoneNumber")
                e.Column.Visibility = Visibility.Collapsed;
        }

        private SolidColorBrush brushAdministrators = new SolidColorBrush(Color.FromArgb(25, 255, 127, 80));
        private SolidColorBrush brushManagers = new SolidColorBrush(Color.FromArgb(100, 255, 228, 181));
        private SolidColorBrush brushStockmans = new SolidColorBrush(Color.FromArgb(35, 125, 227, 210));
        private SolidColorBrush brushNormal = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));

        private void dataGridUsers_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            AdministratorEditRegistrationData data = (AdministratorEditRegistrationData)e.Row.DataContext;
            if (data.Role == "Administrators") e.Row.Background = brushAdministrators;
            else if (data.Role == "Managers") e.Row.Background = brushManagers;
            else if (data.Role == "Stockmans") e.Row.Background = brushStockmans;
            else e.Row.Background = brushNormal;
        }

        private void buttonNew_Click(object sender, RoutedEventArgs e)
        {
            
        }

    }
}
